package com.jiukaopu.base;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.jiukaopu.R;

public abstract class AppBaseActivity extends FragmentActivity {

	protected Button btnTitleLeft, btnTitleRight;
	protected TextView tvTitleLable;

	@Override
	protected final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(fromLayout());

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_base);
		tvTitleLable = (TextView) getWindow().findViewById(R.id.titlebar_lable);
		btnTitleLeft = (Button) getWindow()
				.findViewById(R.id.titlebar_btn_left);
		btnTitleLeft.setOnClickListener(titleButtonClickListener);
		btnTitleRight = (Button) getWindow().findViewById(
				R.id.titlebar_btn_right);
		btnTitleRight.setOnClickListener(titleButtonClickListener);

		intiOnCreate(savedInstanceState);
	}

	public abstract int fromLayout();

	public abstract void intiOnCreate(Bundle savedInstanceState);

	public abstract void onTitleButtonClick(boolean leftClick, View view);

	public void setTitleLable(String title) {
		tvTitleLable.setText(title);
	}

	/**
	 * 
	 * @param buttonType
	 *            0-normal, 1-back
	 * @param text
	 */
	public void setTitleLeftButtonType(int buttonType, String text) {
		btnTitleLeft.setText(text);
		if (buttonType == 0) {
			btnTitleLeft.setCompoundDrawables(null, null, null, null);
		} else if (buttonType == 1) {
			Drawable drawable = getResources()
					.getDrawable(R.drawable.ic_back);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			btnTitleLeft.setCompoundDrawables(drawable, null, null, null);
			btnTitleLeft.requestLayout();
		}
	}

	public void setTitleButtonVisable(int leftVisable, int rightVisable) {
		btnTitleLeft.setVisibility(leftVisable);
		btnTitleRight.setVisibility(rightVisable);
	}

	/**
	 * 
	 * @param buttonType
	 *            0-normal, 1-back
	 * @param text
	 */
	public void setTitleRightButtonType(int buttonType, String text) {
		btnTitleRight.setText(text);
		if (buttonType == 0) {
			btnTitleRight.setCompoundDrawables(null, null, null, null);
		} else if (buttonType == 1) {
			Drawable drawable = getResources()
					.getDrawable(R.drawable.ic_back);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			btnTitleRight.setCompoundDrawables(null, null, drawable, null);
			btnTitleRight.requestLayout();
		}
	}

	private OnClickListener titleButtonClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			onTitleButtonClick(btnTitleLeft == v, v);
		}
	};
}
